package com.cjk.foundation.dao;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.cjk.foundation.entity.DeviceEntity;
import com.cjk.foundation.enums.ConfStatus;
import com.cjk.foundation.enums.DeviceStatus;
import com.cjk.foundation.enums.DeviceType;

@Repository
public interface DeviceDao {

	public void insert(DeviceEntity deviceEntity);

	public void update(DeviceEntity deviceEntity);

	public List<DeviceEntity> selectListByCondition(@Param("userId") Integer userId,
			@Param("groupIds") List<Integer> groupIds, @Param("deviceType") DeviceType deviceType,
			@Param("status") DeviceStatus status, @Param("keywords") String keywords, @Param("page") Integer page,
			@Param("pageSize") Integer pageSize);

	public Integer countByCondition(@Param("userId") Integer userId, @Param("groupIds") List<Integer> groupIds,
			@Param("deviceType") DeviceType deviceType, @Param("status") DeviceStatus status,
			@Param("keywords") String keywords);

	public void deleteBySns(@Param("sns") List<String> sns, @Param("userId") Integer userId);

	public DeviceEntity selectBySn(@Param("sn") String sn, @Param("userId") Integer userId);

	public List<DeviceEntity> selectListBySns(@Param("userId") Integer userId, @Param("sns") List<String> sns);

	@Select("select tenant_name from device where sn = #{sn}")
	public String selectTenantBySn(@Param("sn") String sn);

	@Update("update device set tenant_name = #{tenantName} where sn = #{sn}")
	public void updateTenantNameBySn(@Param("sn") String sn, @Param("tenantName") String tenantName);

	@Update("update device set status = #{status}, conf_status = #{confStatus}, channel_id = null, channel_name = null, tenant_name = null where last_upload_log_time < #{date} or last_upload_log_time is null")
	public void offLine(@Param("status") DeviceStatus status, @Param("confStatus") ConfStatus confStatus,
			@Param("date") Date date);
	
	@Update("update device set channel_id = null, channel_name = null where last_play_time < #{date} or last_upload_log_time is null")
	public void stopPlay(@Param("date") Date date);
	
	public List<String> selectSnsByGroupIdIncSub(@Param("groupId") Integer groupId,
			@Param("deviceType") DeviceType deviceType);
}
